/*
 * MIT License
 *
 * Copyright (c) 2024-2048 冰羽
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package cn.star.web.shell.config;

import cn.hutool.core.collection.CollectionUtil;
import cn.star.framework.rbac.entity.Menu;
import cn.star.framework.rbac.entity.Role;
import cn.star.framework.rbac.entity.RoleResource;
import cn.star.framework.rbac.entity.User;
import cn.star.framework.rbac.entity.UserRole;
import cn.star.framework.rbac.type.ResourceType;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

/**
 * 初始化数据<br>
 *
 * @author zhaoweiping
 *     <p style='color: red'>Created on 2024-09-23 11:06:00
 * @since 3.0.0
 */
@Configuration
@ConfigurationProperties(prefix = "star.init")
public class Config {
  @Getter @Setter private boolean enabled = false;
  @Getter @Setter private String tenantId = "default";

  @Setter private User user;
  @Setter private Role role;

  @Setter private List<User> users = new ArrayList<>();
  @Setter private List<Role> roles = new ArrayList<>();

  @Setter private List<Menu> menus;

  public User getUser() {
    user = user == null ? _default.getDefaultUser() : user;
    user.setTenantId(tenantId);

    return user;
  }

  public Role getRole() {
    role = role == null ? _default.getDefaultRole() : role;
    role.setTenantId(tenantId);

    return role;
  }


  public List<User> getUsers() {
    User item = this.getUser();
    if (item != null
        && users.stream().filter(i -> item.getId().equals(i.getId())).findFirst().orElse(null)
            == null) {
      users.add(item);
    }

    return users;
  }

  public List<Role> getRoles() {
    Role item = this.getRole();
    if (item != null
        && roles.stream().filter(i -> item.getId().equals(i.getId())).findFirst().orElse(null)
            == null) {
      roles.add(item);
    }

    return roles;
  }



  public List<Menu> getMenus() {
    if (CollectionUtil.isNotEmpty(menus)) {
      menus.forEach(
          i -> {
            i.setType(ResourceType.MENU);
            i.setTenantId(tenantId);
          });
    }

    return menus;
  }

  public List<UserRole> getUserRoles() {
    UserRole item = new UserRole();
    item.setUserId(this.getUser().getId());
    item.setRoleId(this.getRole().getId());

    return CollectionUtil.newArrayList(item);
  }

  public List<RoleResource> getRoleResources() {
    Role item = this.getRole();
    List<Menu> menus = this.getMenus();

    return menus.stream()
        .map(
            i -> {
              RoleResource rel = new RoleResource();
              rel.setRoleId(item.getId());
              rel.setResourceId(i.getId());
              rel.setResourceType(ResourceType.MENU);

              return rel;
            })
        .collect(Collectors.toList());
  }
}
